//
//  WQMyMoreTableCell.m
//  WisdomQuestor
//
//  Created by 纪景城 on 2024/12/19.
//

#import "WQMyMoreTableCell.h"
#import "WQMySelectRedCell.h"
#import <ThingSmartBizCore/ThingSmartBizCore.h>
#import <ThingModuleServices/ThingSmartHomeDataProtocol.h>
#import <ThingModuleServices/ThingMessageCenterProtocol.h>
#import <ThingModuleServices/ThingHelpCenterProtocol.h>

typedef NS_ENUM(NSUInteger, MyMoreCellType) {
    MyMoreCellTypeJiaTingGuanLi, // 家庭管理
    MyMoreCellTypeMessage,
    MyMoreCellTypeQuestion,
    MyMoreCellTypeHomeKit,
};

@interface WQMyMoreTableCell () <UITableViewDelegate, UITableViewDataSource>

@end

@implementation WQMyMoreTableCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.clipsToBounds = YES;
        self.backgroundColor = UIColor.clearColor;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self.contentView addSubviews:[NSArray arrayWithObjects:self.jk_tableView, nil]];
        [self fragmentUI_ViewSet];
        
        // 注册要实现的协议
        [[ThingSmartBizCore sharedInstance] registerService:@protocol(ThingSmartHomeDataProtocol) withInstance:self];
        
    }
    return self;
}

#pragma mark ------- Click Methods -------

// 实现对应的协议方法
- (ThingSmartHome *)getCurrentHome {
    ThingSmartHome *home = [ThingSmartHome homeWithHomeId:@"11"];
    return home;
}

- (void)gotoMessageCenterViewControllerWithAnimated {
    id<ThingMessageCenterProtocol> impl = [[ThingSmartBizCore sharedInstance] serviceOfProtocol:@protocol(ThingMessageCenterProtocol)];
    [impl gotoMessageCenterViewControllerWithAnimated:YES];
}


#pragma mark ------- UITableViewDelegate -------

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kFragmemtFloat(60);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WQMySelectRedCell *cell = [WQMySelectRedCell ff_CellDequeueWithId:tableView];
    
    switch (indexPath.section) {
            
        case MyMoreCellTypeJiaTingGuanLi:
        {
            cell.iconImageView.image = kImageName(@"me_morelist_home");
            cell.nameLabel.text = @"家庭管理";
            cell.redView.hidden = YES;
        }
            break;
            
        case MyMoreCellTypeMessage:
        {
            cell.iconImageView.image = kImageName(@"me_morelist_message");
            cell.nameLabel.text = @"消息中心";
            cell.redView.hidden = NO;
        }
            break;
            
        case MyMoreCellTypeQuestion:
        {
            cell.iconImageView.image = kImageName(@"me_morelist_question");
            cell.nameLabel.text = @"常见问题与反馈";
            cell.redView.hidden = YES;
        }
            break;
            
        case MyMoreCellTypeHomeKit:
        {
            cell.iconImageView.image = kImageName(@"me_morelist_kit");
            cell.nameLabel.text = @"HomeKit 信息";
            cell.redView.hidden = YES;
        }
            break;
            
        default:
            break;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    switch (indexPath.section) {
            
        case MyMoreCellTypeJiaTingGuanLi:
        {
            if (WQShare.isLoginUserAndPresent) {
                id<ThingFamilyProtocol> impl = [[ThingSmartBizCore sharedInstance] serviceOfProtocol:@protocol(ThingFamilyProtocol)];
                if ([impl respondsToSelector:@selector(gotoFamilyManagement)]) {
                    [impl gotoFamilyManagement];
                }
            }
        }
            break;
            
        case MyMoreCellTypeMessage:
        {
            if (WQShare.isLoginUserAndPresent) {
                [self gotoMessageCenterViewControllerWithAnimated];
            }
        }
            break;
            
        case MyMoreCellTypeQuestion:
        {
            if (WQShare.isLoginUserAndPresent) {
                id<ThingHelpCenterProtocol> impl = [[ThingSmartBizCore sharedInstance] serviceOfProtocol:@protocol(ThingHelpCenterProtocol)];
                [impl gotoHelpCenter];
            }
        }
            break;
            
            
        default:
            break;
    }
}

#pragma mark ------- Lazy Methods -------

- (void)fragmentUI_ViewSet {
    [self.jk_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).offset(kFragmemtFloat(16));
        make.right.equalTo(self.contentView.mas_right).offset(-kFragmemtFloat(16));
        make.top.bottom.equalTo(self.contentView);
    }];
}

- (UITableView *)jk_tableView {
    if (!_jk_tableView) {
        _jk_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _jk_tableView.delegate = self;
        _jk_tableView.dataSource = self;
        
        _jk_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _jk_tableView.backgroundColor = [UIColor whiteColor];
        _jk_tableView.cornerRadius = kFragmemtFloat(10);
        
        _jk_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        _jk_tableView.showsVerticalScrollIndicator = NO;
        _jk_tableView.showsHorizontalScrollIndicator = NO;
        if(@available(iOS 15.0,*)){_jk_tableView.sectionHeaderTopPadding=0;}
    }
    return _jk_tableView;
}

@end
